package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;

@SpringBootTest
public class testHash {

    @Autowired
    private RedisTemplate<String,String> redisTemplate; // redisTemplate是springboot自动注入的


    @Test
    public void testHash(){
        //hash请求   大key 小key  value
        redisTemplate.opsForHash().put("user","name","张三");
        redisTemplate.opsForHash().put("user","age","30");
        System.out.println("===================================");
        //根据大key和小key获取值
        System.out.println(redisTemplate.opsForHash().get("user", "name"));
        System.out.println("===================================");
        //根据大key获取所有的小key
        Set<Object> keys = redisTemplate.opsForHash().keys("user");
        System.out.println(keys);
        System.out.println("===================================");
        //根据大key获取所有的值
        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);
        System.out.println("===================================");
        //删除小key和值
        redisTemplate.opsForHash().delete("user","age");

    }
}